package com.have.blog.admin.redis.impl;

import com.have.blog.admin.constant.AdminRedisConstant;
import com.have.blog.admin.redis.WebVisitCacheService;
import com.have.blog.model.models.Admin;
import com.have.blog.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created By Have
 * 2021/7/17 22:16
 */
@Service
public class WebVisitCacheServiceImpl implements WebVisitCacheService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Value("${redis.admin.database}")
    private String REDIS_KEY_DATABASE;

    @Override
    public List<Long> getUvByWeek() {
        ArrayList<String> resultSevenDayList = DateUtils.getDaysByN(7, "MM-dd");
        List<Long> result = new ArrayList<>();
        for (String day : resultSevenDayList) {
            String key = REDIS_KEY_DATABASE + AdminRedisConstant.SEGMENTATION
                    + AdminRedisConstant.UV + AdminRedisConstant.SEGMENTATION
                    + day;

            long size = redisTemplate.opsForValue().get(key) == null? 0 : Long.valueOf((String) redisTemplate.opsForValue().get(key));

            result.add(size);
        }
        return result;
    }

    @Override
    public List<Long> getPvByWeek() {
        ArrayList<String> resultSevenDayList = DateUtils.getDaysByN(7, "MM-dd");
        List<Long> result = new ArrayList<>();
        for (String day : resultSevenDayList) {
            String key = REDIS_KEY_DATABASE + AdminRedisConstant.SEGMENTATION
                    + AdminRedisConstant.PV + AdminRedisConstant.SEGMENTATION
                    + day;
            long size = redisTemplate.opsForValue().get(key) == null? 0 : Long.valueOf((String) redisTemplate.opsForValue().get(key));
            result.add(size);
        }
        return result;
    }
}
